<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: pp <124910168@qq.com>
// +----------------------------------------------------------------------

namespace app\home\controller;

use think\Db;
use think\Controller;
use controller\BasicHome;

/**
 * 频道-学习课件
 * Class Courseware
 * @package app\home\controller
 * @author pp <124910168@qq.com>
 * @date 2020/08/15 10:41
 */
class Courseware extends BasicHome
{
    public function _initialize()
    {
        parent::_initialize();
        
        define('ACTION_NAME',$action);
        define('MODULE_NAME',$controller);
    }

    //首页
    public function index(){
        $get = $this->request->get();
        $catid = input('cid', 2);//默认 图文 理论知识
        // halt($catid);
        
        $map = ['status' => 1, 'catid' => $catid];
        $m = $this->category->getValue(['id' => $catid], 'module');

        // 应用搜索条件
        foreach (['kw', 'aid', 'userid'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if(in_array($key, ['aid', 'userid'])){
                    $map[$key] = [
                        ['eq', $get[$key]],
                        ['like', $get[$key].",%"],
                        ['like', "%,".$get[$key].",%"],
                        ['like', "%,".$get[$key]],
                        'or',
                    ];
                }else{
                    $uids = Db::name('Client')->where('username', 'like', "%{$get[$key]}%")->column('id');
                    if($uids){
                        $map['userid'] = ['in', $uids];
                    }else{
                        $map['title|username|keywords|description|content'] = ['like', "%".$get[$key]."%"];
                    }
                }
            }
        }

        // $list = $this->demonstration->getPageLists($map, '', '');

        $list = Db::name($m)
        ->where($map)
        ->order('sort asc,createtime desc')
        ->paginate(config('pageSize'));
        // halt($list);
        
        // 获取分页显示
        $page = $list->render();
        $list = $list->toArray();
        // halt($list['data']);

        $data = [
            'list' => $list['data'],
            'page' => $page,
            'kw' => !empty($get['kw']) ? $get['kw'] : '',
            'aid' => !empty($get['aid']) ? $get['aid'] : '',
            'nid' => !empty($get['nid']) ? $get['nid'] : '',
            'did' => !empty($get['did']) ? $get['did'] : '',
            'cid' => $catid,
        ];
        $cattemplate = $this->category->where('id',$catid)->value('template_list');
        $template = $cattemplate ? $cattemplate : 'article_list';
        // halt($template);
        return $this->fetch('./view/'.$template, $data);
    }

    //列表
    public function list(){
        if(!input('cid')){
            $this->error('找不到数据');
        }
        $map = $this->map;
        $map['catid'] = input('cid');
        $map1 = $this->map1;
        $map1['catid'] = input('cid');
        $list = $this->article
                ->where($map)
                ->whereOr(function ($q) use($map1) {
                        $q->where($map1);
                    })
                ->order('id desc')
                ->select();
        $catname = $this->category->where(['id' => input('cid')])->value('catname');
        $data = [
            'catname' => $catname,
            'data' => $list,
        ];
        return $this->fetch('', $data);
    }

    //详情
    public function detail(){
        // $this->_ckR();
        if(!input('cid')){
            $this->error('找不到数据');
        }
        if(!input('id')){
            $this->error('找不到数据');
        }
        $m = $this->category->getValue(['id' => input('cid')], 'module');
        // halt($m);
        $info = Db::name($m)->where(['id' => input('id')])->find();
        if($info['status'] != 1){
            $this->error('文章未审核，请联系管理员');
        }
        Db::name($m)->where(['id' => input('id')])->setInc('hits');
        // if($info['files']){
        //     $info['files'] = explode(';', $info['files']);
        // }
        if($info['pics']){
            $info['pics'] = explode(';', $info['pics']);
        }

        //上一页
        $prev = Db::name($m)->where(['status' => 1, 'catid' => $info['catid'], 'id' => ['lt', input('id')]])->find();
        //下一页
        $next = Db::name($m)->where(['status' => 1, 'catid' => $info['catid'], 'id' => ['gt', input('id')]])->find();

        $data = [
            'info' => $info,
            'prev' => $prev,
            'next' => $next,
        ];
        $cattemplate = $this->category->where('id',input('cid'))->value('template_show');
        $template = $cattemplate ? $cattemplate : 'article_show';
        // halt($info);
        return $this->fetch('./view/'.$template, $data);
    }

}
